# This file is part of BlackArch Linux ( https://www.blackarch.org/ ).
# See COPYING for license details.

pkgname=bloodhound
pkgver=v4.3.1.r60.g226f497
pkgrel=2
epoch=1
pkgdesc='Six Degrees of Domain Admin'
groups=('blackarch' 'blackarch-recon' 'blackarch-windows')
arch=('x86_64' 'aarch64')
url='https://github.com/SpecterOps/BloodHound-Legacy'
license=('GPL-3.0-only')
depends=('nodejs' 'neo4j-community' 'jre17-openjdk-headless')
makedepends=('git' 'nodejs-electron-packager' 'npm' 'unzip')
optdepends=('python: for some scripts' 'powershell: for PowerShell ingestion')
source=("$pkgname::git+$url.git")
options=(!strip)
sha512sums=('SKIP')
install="$pkgname.install"

if [ $CARCH = 'x86_64' ]
then
  _path=BloodHound-linux-x64
else
  _path=BloodHound-linux-arm64
fi

pkgver() {
  cd $pkgname

  ( set -o pipefail
    git describe --long --tags --abbrev=7 2>/dev/null |
      sed 's/\([^-]*-g\)/r\1/;s/-/./g' ||
    printf "%s.%s" "$(git rev-list --count HEAD)" \
      "$(git rev-parse --short=7 HEAD)"
  )
}

build() {
  cd $pkgname

  npm install
  NODE_OPTIONS=--openssl-legacy-provider npm run build
}

package() {
  cd $pkgname

  install -dm 755 "$pkgdir/usr/bin"
  install -dm 755 "$pkgdir/usr/share/$pkgname"

  install -Dm 644 -t "$pkgdir/usr/share/doc/$pkgname/" README.md

  cp -a --no-preserve=ownership docs/ "$pkgdir/usr/share/doc/$pkgname/"

  rm -r *.md docs/

  cp -a --no-preserve=ownership $_path Collectors/ \
    src/ *.js *.json *.html "$pkgdir/usr/share/$pkgname/"
  if [ $CARCH = 'x86_64' ]
  then
    cp -a --no-preserve=ownership dist/ "$pkgdir/usr/share/$pkgname/"
  fi

  cat > "$pkgdir/usr/bin/$pkgname" << EOF
#!/bin/sh
cd /usr/share/$pkgname/$_path
echo "To start neo4j: systemctl start neo4j"
exec ./BloodHound --no-sandbox "\$@"
EOF

  chmod +x "$pkgdir/usr/bin/$pkgname"
  chmod +x "$pkgdir/usr/share/$pkgname/$_path/BloodHound"
}

